//
//  SABatchPurchaseVC.m
//  saasRetail
//
//  Created by mac on 2022/9/15.
//

#import "SABatchPurchaseVC.h"
#import "SABatchPurchaseDetailVC.h"
#import "SATutorialVC.h"
#import "SAOcrScanAPI.h"
#import <TZImagePickerController.h>
#import "SAOcrScanResultModel.h"
#import "SAShopPreviewVC.h"

@interface SABatchPurchaseVC ()<HTTPAPIDelegate,TZImagePickerControllerDelegate>

@property (nonatomic, strong) SAOcrScanAPI *ocrScanAPI;
@property (nonatomic, assign) CGFloat scale;

@end

@implementation SABatchPurchaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [HDHubView dismissLoading];
    if (self.ocrScanAPI == api) {
        NSMutableArray<SAOcrScanResultModel *> *marr = [SAOcrScanResultModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        SABatchPurchaseDetailVC *vc = [SABatchPurchaseDetailVC new];
        vc.dataMarr = marr;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [HDHubView dismissLoading];
}

#pragma mark - event
-(void)clickedSetBtn {
    [self.view endEditing:YES];
    
//    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
//    NSString *purchaseReceipt = [NSString stringWithFormat:@"%@",menuList[2][@"perms"][@"purchaseReceipt"]];
//    if ([purchaseReceipt isEqualToString:@"1"]) {
//        [Prompt popPromptViewWithMsg:@"您没有入库单据权限,请联系店长添加" duration:2];
//        return;;
//    }
    
    SAShopPreviewVC *pushVC = [SAShopPreviewVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedCourseLab {
    [self.navigationController pushViewController:[SATutorialVC new] animated:YES];
}

-(void)clickedUploadBtn:(UIButton *)sender {
    
//    if (sender.tag == 101) {
//        self.scale = 1.0;
//    }else {
//        self.scale = 0.5;
//    }
    
    self.scale = 1.0;
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:5 delegate:self pushPhotoPickerVc:YES];
    imagePickerVc.photoWidth = kScreenWidth * 4;
    imagePickerVc.photoPreviewMaxWidth = kScreenWidth * 4;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photo, NSArray * assets, BOOL isSelectOriginalPhoto) {
        for (NSInteger i = 0; i<photo.count; i++) {
        }
        
    }];
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
    
    if (photos.count > 0) {
        [HDHubView loadingMessage:@"图片正在识别,请稍等..."];
        [self.ocrScanAPI OCRImageData:photos AndScale:self.scale];
    }
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //让屏幕旋转失效
    [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeRotate" object:@"1"];
}



- (void)imagePickerControllerDidCancel:(TZImagePickerController *)picker{
    //让屏幕旋转失效
    [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeRotate" object:@"1"];
}

#pragma mark - initUI
-(void)initUI {
    
    [self setNavigationItemBackBBIAndTitle:@"小票入库"];
    [self setBgImage:nil];
    
//    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    setBtn.frame = CGRectMake(0, 0, kScaleNum(60), kScaleNum(44));
//    [setBtn setImage:[UIImage imageNamed:@"shopSale_list"] forState:UIControlStateNormal];
//    [setBtn addTarget:self action:@selector(clickedSetBtn) forControlEvents:UIControlEventTouchUpInside];
//    [self addRightViews:@[setBtn]];
    
    UIImageView *bigImageV = [[UIImageView alloc] initWithFrame:SARectMake(25, StatusHeight, 325, 450)];
    bigImageV.image = [UIImage imageNamed:@"pageDetail"];
    bigImageV.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:bigImageV];
    
    UILabel *tipLab = [[UILabel alloc] initWithFrame:SARectMake(0, StatusHeight + 460, kBaseScreenWidth, 40)];
    tipLab.font = kFont(16);
    tipLab.textAlignment = NSTextAlignmentCenter;
    
    tipLab.text = @"支持**花\n电子进货单截图自动识别进货";
    tipLab.numberOfLines = 0 ;
    tipLab.textColor = kBlackColor;
    [self.view addSubview:tipLab];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadBtn.frame = SARectMake(90, StatusHeight + 510, 195, 40);
    [uploadBtn setTitle:@"上传电子进货单截图" forState:UIControlStateNormal];
    [uploadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    uploadBtn.backgroundColor = kBuleColor;
    uploadBtn.layer.masksToBounds = YES;
    uploadBtn.titleLabel.font = kFont(16);
    uploadBtn.layer.cornerRadius = 5 * kScreenScale;
    [uploadBtn addTarget:self action:@selector(clickedUploadBtn:) forControlEvents:UIControlEventTouchUpInside];
    uploadBtn.tag = 101;
    [self.view addSubview:uploadBtn];
    
    UIButton *uploadBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadBtn2.frame = SARectMake(kBaseScreenWidth - 60, StatusHeight + 510, 50, 40);
    [uploadBtn2 setTitle:@"0.5" forState:UIControlStateNormal];
    [uploadBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    uploadBtn2.backgroundColor = kBuleColor;
    uploadBtn2.layer.masksToBounds = YES;
    uploadBtn2.titleLabel.font = kFont(16);
    uploadBtn2.layer.cornerRadius = 5 * kScreenScale;
    uploadBtn2.tag = 102;
    [uploadBtn2 addTarget:self action:@selector(clickedUploadBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:uploadBtn2];
    
    UILabel *courseLab = [[UILabel alloc] initWithFrame:SARectMake(125, StatusHeight + 560, 125, 40)];
    courseLab.font = kFont(16);
    courseLab.textAlignment = NSTextAlignmentCenter;
    courseLab.text = @"查看教程>>";
    courseLab.numberOfLines = 0 ;
    courseLab.textColor = kBuleColor;
    courseLab.userInteractionEnabled = YES;
    [self.view addSubview:courseLab];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedCourseLab)];
    [courseLab addGestureRecognizer:tap];
}

-(SAOcrScanAPI *)ocrScanAPI {
    if (!_ocrScanAPI) {
        _ocrScanAPI = [SAOcrScanAPI apiWithDelegate:self];
    }
    return _ocrScanAPI;
}

@end
